import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { z } from '#/adapter/form';
import { CommonStatusEnum, DICT_TYPE, getDictOptions } from '#/utils';

// 物理资源类型枚举
export enum PhysicalResourceTypeEnum {
  APPLICATION = 'APPLICATION',
  APPROVAL = 'APPROVAL',
  RESOURCE = 'RESOURCE',
}

// 物理资源API类型定义
export interface PhysicalResourceApi {
  Resource: {
    capacity?: number;
    children?: PhysicalResourceApi.Resource[];
    code: string;
    description: string;
    id: string;
    level: number;
    location?: string;
    manufacturer?: string;
    name: string;
    parentId: null | string;
    purchaseDate?: string;
    resourceType: PhysicalResourceTypeEnum;
    serialNumber: string;
    specifications?: string;
    status: CommonStatusEnum;
    warrantyPeriod?: string;
  };
}

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'parentId',
      label: '上级资源',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          // TODO: 调用获取物理资源列表API
          return [];
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择上级资源',
        showSearch: true,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'name',
      label: '资源名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入资源名称',
      },
      rules: 'required',
    },
    {
      fieldName: 'code',
      label: '资源编码',
      component: 'Input',
      componentProps: {
        placeholder: '请输入资源编码',
      },
      rules: 'required',
    },
    {
      fieldName: 'resourceType',
      label: '资源类型',
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: '应用', value: PhysicalResourceTypeEnum.APPLICATION },
          { label: '资源', value: PhysicalResourceTypeEnum.RESOURCE },
          { label: '审批', value: PhysicalResourceTypeEnum.APPROVAL },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.string().default(PhysicalResourceTypeEnum.APPLICATION),
    },
    {
      fieldName: 'status',
      label: '资源状态',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'description',
      label: '资源描述',
      component: 'InputTextArea',
      componentProps: {
        placeholder: '请输入资源描述',
        rows: 3,
      },
    },
    {
      fieldName: 'location',
      label: '资源位置',
      component: 'Input',
      componentProps: {
        placeholder: '请输入资源位置',
      },
    },
    {
      fieldName: 'capacity',
      label: '资源容量',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        controlsPosition: 'right',
        placeholder: '请输入资源容量',
      },
    },
    {
      fieldName: 'specifications',
      label: '规格参数',
      component: 'Input',
      componentProps: {
        placeholder: '请输入规格参数',
      },
    },
    {
      fieldName: 'manufacturer',
      label: '制造商',
      component: 'Input',
      componentProps: {
        placeholder: '请输入制造商',
      },
    },
    {
      fieldName: 'purchaseDate',
      label: '购买日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择购买日期',
        format: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'warrantyPeriod',
      label: '保修期',
      component: 'Input',
      componentProps: {
        placeholder: '请输入保修期',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<
  PhysicalResourceApi['Resource']
>['columns'] {
  return [
    {
      field: 'name',
      title: '资源名称',
      align: 'left',
      fixed: 'left',
      slots: { default: 'name' },
      treeNode: true,
    },
    {
      field: 'code',
      title: '资源编码',
    },
    {
      field: 'resourceType',
      title: '资源类型',
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            { label: '应用', value: PhysicalResourceTypeEnum.APPLICATION },
            { label: '资源', value: PhysicalResourceTypeEnum.RESOURCE },
            { label: '审批', value: PhysicalResourceTypeEnum.APPROVAL },
          ],
        },
      },
    },
    {
      field: 'serialNumber',
      title: '序号',
    },
    {
      field: 'location',
      title: '位置',
    },
    {
      field: 'status',
      title: '状态',
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      title: '操作',
      width: 220,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
